package com.frozenlab.android.wheretoeatai;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class FoodPlaceSummary extends Activity {
	
    private static final int ACTIVITY_EDIT=1;
    
    private static final int EDIT_ID = Menu.FIRST;
    private static final int DELETE_ID = Menu.FIRST + 1;
	
	/* Database helper */
	private FoodPlaceDbHelper dbHelper;

	/* Variable for database table fields */
    private Long rowId;
    private TextView name;
	private CheckBox checkBoxBreakfast;
	private CheckBox checkBoxLunch;
	private CheckBox checkBoxDinner;
	private CheckBox checkBoxSupper;
	private CheckBox checkBoxRefreshment;
	private CheckBox checkBoxOthers;
	private TextView priceRange;

    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dbHelper = new FoodPlaceDbHelper(this);
        dbHelper.open();
        setContentView(R.layout.food_place_summary);
        
        name = (TextView) findViewById(R.id.textName);
    	checkBoxBreakfast = (CheckBox) findViewById(R.id.checkBoxBreakfast);
    	checkBoxLunch = (CheckBox) findViewById(R.id.checkBoxLunch);
    	checkBoxDinner = (CheckBox) findViewById(R.id.checkBoxDinner);
    	checkBoxSupper = (CheckBox) findViewById(R.id.checkBoxSupper);
    	checkBoxRefreshment = (CheckBox) findViewById(R.id.checkBoxRefreshment);
    	checkBoxOthers = (CheckBox) findViewById(R.id.checkBoxOthers);
    	priceRange = (TextView) findViewById(R.id.priceRange);
        
        Bundle extras = getIntent().getExtras();
        rowId = extras.getLong(FoodPlaceDbHelper.KEY_ROWID);
        populateFields();
    }
    
    private void populateFields() {
        if (rowId != null) {
            Cursor cursor = dbHelper.readRecord(rowId);
            startManagingCursor(cursor);
            name.setText(cursor.getString(cursor.getColumnIndexOrThrow(FoodPlaceDbHelper.KEY_NAME)));
            if(cursor.getString(cursor.getColumnIndexOrThrow(FoodPlaceDbHelper.KEY_IS_BREAKFAST)).compareTo("1")==0){
            	checkBoxBreakfast.setChecked(true);
            }else{checkBoxBreakfast.setChecked(false);}
            if(cursor.getString(cursor.getColumnIndexOrThrow(FoodPlaceDbHelper.KEY_IS_LUNCH)).compareTo("1")==0){
            	checkBoxLunch.setChecked(true);
            }else{checkBoxLunch.setChecked(false);}
            if(cursor.getString(cursor.getColumnIndexOrThrow(FoodPlaceDbHelper.KEY_IS_DINNER)).compareTo("1")==0){
            	checkBoxDinner.setChecked(true);
            }else{checkBoxDinner.setChecked(false);}
            if(cursor.getString(cursor.getColumnIndexOrThrow(FoodPlaceDbHelper.KEY_IS_SUPPER)).compareTo("1")==0){
            	checkBoxSupper.setChecked(true);
            }else{checkBoxSupper.setChecked(false);}
            if(cursor.getString(cursor.getColumnIndexOrThrow(FoodPlaceDbHelper.KEY_IS_REFRESHMENT)).compareTo("1")==0){
            	checkBoxRefreshment.setChecked(true);
            }else{checkBoxRefreshment.setChecked(false);}
            if(cursor.getString(cursor.getColumnIndexOrThrow(FoodPlaceDbHelper.KEY_IS_OTHERS)).compareTo("1")==0){
            	checkBoxOthers.setChecked(true);
            }else{checkBoxOthers.setChecked(false);}
            priceRange.setText(cursor.getString(cursor.getColumnIndexOrThrow(FoodPlaceDbHelper.KEY_PRICE_RANGE)));
        }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, EDIT_ID, 0, R.string.menu_edit);
        menu.add(0, DELETE_ID, 1, R.string.menu_delete);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	Intent intent;
        switch(item.getItemId()) {
            case EDIT_ID:
            	intent = new Intent(this, FoodPlaceEdit.class);
            	intent.putExtra(FoodPlaceDbHelper.KEY_ROWID, rowId);
            	startActivityForResult(intent, ACTIVITY_EDIT);
            	break;
            case DELETE_ID:
            	showDeleteAlert();
            	break;
        }
        return super.onMenuItemSelected(featureId, item);
    }
    
	public void showDeleteAlert(){
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setMessage(R.string.Delete_alert_message)
    	       .setCancelable(false)
    	       .setPositiveButton(R.string.Yes, new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	        	   dbHelper.deleteRecord(rowId);
    	        	   Toast.makeText(getApplicationContext(), R.string.Food_place_deleted, Toast.LENGTH_SHORT).show();
    	        	   finish();
    	           }
    	       })
    	       .setNegativeButton(R.string.No, new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	                dialog.cancel();
    	           }
    	       });
    	AlertDialog alert = builder.create();
    	alert.show();
	}
	
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        populateFields();
    }
}
